<template>
  <div class="login-container">
    <div class="logo-container">
      <div class="logo">
        <img src="@/assets/logo.png" alt="Logo">
      </div>
      <h1 class="app-name">考试系统</h1>
    </div>
    <div class="form-container">
      <van-field
        v-model="username"
        placeholder="请输入用户名"
        :border="false"
        class="input-field"
        prefix-icon="user">
      </van-field>
      <van-field
        v-model="password"
        type="password"
        placeholder="请输入密码"
        :border="false"
        class="input-field"
        prefix-icon="lock">
      </van-field>
      <van-button
        type="primary"
        block
        class="login-button"
        @click="handleLogin"
      >
        登录
      </van-button>
      <div class="register-link">
        还没有账号？<router-link to="/reg">立即注册</router-link>
      </div>
    </div>
  </div>
</template>

<script>
import { useRouter } from 'vue-router'
import { showToast } from 'vant'
import { login } from "@/api/userApi.js"

export default {
  name: 'LoginPage',
  data() {
    return {
      username: 'fyx123fyx123',
      password: '123123'
    }
  },
  methods: {
    handleLogin() {
      if (this.username && this.password) {
        login({
          username: this.username,
          password: this.password
        }).then(res => {
          if (res && res.token) {
            // 保存token到本地存储
            console.log(res)
            localStorage.setItem('token', res.token);
            localStorage.setItem('user', JSON.stringify(res.user));
            console.log(JSON.parse(localStorage.getItem('user')))
            showToast('登录成功')
            this.$router.push('/home')
          } else {
            showToast(res.message || res.msg || '登录失败')
          }
        })
      } else {
        showToast('请输入用户名和密码')
      }
    }
  },
  created() {
    // 初始化路由
    this.$router = useRouter()
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 0 20px;
}

.logo-container {
  text-align: center;
  margin-bottom: 40px;
}

.logo {
  width: 100px;
  height: 100px;
  background: white;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0 auto 20px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
}

.logo img {
  width: 60px;
  height: 60px;
}

.app-name {
  color: white;
  font-size: 24px;
  font-weight: bold;
}

.form-container {
  width: 100%;
  max-width: 400px;
  background: white;
  border-radius: 12px;
  padding: 30px 20px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.input-field {
  margin-bottom: 20px;
  background: #f5f5f5;
  border-radius: 8px;
  padding: 0 15px;
}

.login-button {
  margin-top: 30px;
  background: #667eea;
  border-radius: 8px;
  height: 50px;
  line-height: 50px;
  font-size: 16px;
}

.register-link {
  text-align: center;
  margin-top: 20px;
  color: #666;
}

.register-link a {
  color: #667eea;
  text-decoration: none;
}
</style>